<!--
 * @Author: gaoyu gy15354315043@163.com
 * @Date: 2024-10-24 14:47:50
 * @LastEditors: 郝奔 1425466221@qq.com
 * @LastEditTime: 2024-12-13 16:14:14
 * @Description: 九宫格抽屉
 * Copyright (c) 2024-present HBIS Digital Technology Co.,Ltd. All rights reserved. 
-->
<template>
    <DtDrawer ref="dtDrawerRef" v-model="visible" :direction="'ltr'" class="gridDrawer" :with-header="false"
        :size="1192" :isFooterSlot="false" @close="handleClose" :closeOnClickModal="true">
        <div class="gridContent">
            <div class="left_box">
                <div class="menuTitle hoverClass">应用中心</div>
                <div>
                    <div class="menuTitle collect hoverClass">
                        <div>我的收藏</div>
                        <div class="collectManage" @click="handleMenu">管理</div>
                    </div>
                    <div class="collectedMenu" v-if="stateData.collectedMenuList.length > 0">
                        <draggable itemKey="menuId" v-model="stateData.collectedMenuList" @change="changeMenuSort"
                            @start="onStart" @end="onEnd">
                            <template #item="{ element }">
                                <div class="colllectRow" @mouseenter="mouseMenuenter(element)"
                                    @mouseleave="mouseMenuleave"
                                    :class="[stateData.menuActive == element.menuId ? 'menuActive' : 'menuInActive']">
                                    <svg-icon icon-class="move" class="moveIcon"
                                        v-if="stateData.menuActive == element.menuId" />
                                    <div class="collectTitle" @click="handleMenuRoute(element)">{{ element.menuName }}
                                    </div>
                                    <svg-icon icon-class="close" class="closeIcon"
                                        v-if="stateData.menuActive == element.menuId" @click="canceCollect(element)" />
                                </div>
                            </template>
                        </draggable>
                    </div>
                </div>
            </div>
            <div class="right_box">
                <application-list ref="applicationListRef" @closeDrawer="handleClose" :drawerVisible="visible" />
                <collect-management ref="collectManagementRef" @edited="getCollectMenu" />
            </div>
        </div>
    </DtDrawer>
</template>
<script lang="ts" setup>
import ApplicationList from './application-list.vue';
import CollectManagement from './collect-management.vue';
import draggable from 'vuedraggable'
import { getCurrentInstance } from 'vue'
import mitt from '../../../../../utils/mitt';
import { getCollectMenuList, deleteCollectMenu, updateSort } from '../../api/index'
import { collectMenuList } from '../../api/types'
// import { getToken } from '@/framework/utils/auth';
import { openOrPushUrl } from '../../../../../utils/index'
const closeGrid = inject('closeGrid');
const vm = getCurrentInstance()!
const { useDrawer, $modal } = vm.appContext.config.globalProperties
const { visible, openDrawer, closeDrawer } = useDrawer()
const collectManagementRef = ref()
const applicationListRef = ref()
const emits = defineEmits(['changeIcon'])
/**
 * @description: 监听closeNineGrid事件，并关闭九宫格
 * @return {*}
 */
mitt.on('closeNineGrid', () => {
    handleClose()
})
interface dataType {
    menuActive: string;
    collectedMenuList: collectMenuList[];
}
const temporarilyMenuId = ref('')
/**
 * 开始拖拽
 */
const dragStatus = ref(false)
const onStart = (): void => {
    dragStatus.value = true
    temporarilyMenuId.value = stateData.menuActive
}
const onEnd = (): void => {
    stateData.menuActive = temporarilyMenuId.value
    temporarilyMenuId.value = ''
    dragStatus.value = false
}

const stateData = reactive<dataType>({
    menuActive: '',
    collectedMenuList: [],
})
/**
 * @description: 打开弹窗
 * @return {*}
 */
function initData(): void {
    openDrawer()
    getCollectMenu()
    if (!applicationListRef.value) return
    applicationListRef.value.initData()
}
/**
 * @description: 关闭按钮
 * @return {*}
 */
function handleClose(): void {
    closeDrawer()
    emits('changeIcon');
}
/**
* @description: 收藏-点击菜单跳转
* @return {*}
*/
function handleMenuRoute(val: collectMenuList): void {
    // window.open(`${val.path}?token=${getToken()}`)
    // openOrPushUrl(val, val.path)
    openOrPushUrl(val, val.path,closeGrid)
}
/**
 * @description: 拖拽菜单
 * @return {*}
 */
async function changeMenuSort(): Promise<void> {
    await updateSort(stateData.collectedMenuList)
    getCollectMenu()

}
/**
 * @description: 鼠标悬浮到菜单
 * @return {*}
 */
function mouseMenuenter(node: collectMenuList): void {
    if (dragStatus.value) {
        return
    }
    stateData.menuActive = node.menuId
}
/**
 * @description: 鼠标离开菜单
 * @return {*}
 */
function mouseMenuleave(): void {
    stateData.menuActive = '-1'
}
/**
 * @description: 点击“管理”按钮
 * @return {*}
 */
function handleMenu(): void {
    if (!collectManagementRef.value) return
    collectManagementRef.value.openDialog()
    collectManagementRef.value.initData()
}
/**
 * @description: 查询当前登录用户收藏菜单列表
 * @return {*}
 */
async function getCollectMenu(): Promise<void> {
    const res = await getCollectMenuList()
    stateData.collectedMenuList = res.data.map(item => {
        return {
            ...item,
            path: item.appAddress + item.menuPath,
        }
    })
}
/**
 * @description: 取消收藏
 * @return {*}
 */
async function canceCollect(element: collectMenuList): Promise<void> {
    await deleteCollectMenu({ menuCode: element.menuCode })
    $modal.msgSuccess('删除成功')
    getCollectMenu()

}
defineExpose({
    closeDrawer,
    initData
})
</script>
<style scoped lang="scss">
.gridDrawer {

    :deep(.el-overlay) {
        top: 60px;
        overflow: hidden;


        .el-drawer__body {
            height: 100%;
            overflow: hidden;

            .content {
                padding: 0;
                height: 100%;
            }
        }

    }

    .gridContent {
        display: flex;
        height: 100%;

        .left_box {
            width: 240px;
            background: #191919;

            .menuTitle {
                width: 240px;
                box-sizing: border-box;
                padding: 12px 20px;
                color: #FFFFFF;
                opacity: 0.9;
                font-size: 14px;
                line-height: 22px;
            }

            .collect {
                display: flex;
                justify-content: space-between;

                div {
                    opacity: 0.55;
                }

                .collectManage {
                    cursor: pointer;
                }

                .collectManage:hover {
                    opacity: 0.9;
                }
            }

            .hoverClass:hover {
                background: #262727;
            }

            .selectedClass {
                background: #262727;
            }

            .collectedMenu {
                width: 100%;
                height: calc(100vh - 60px - 92px - 38px);
                overflow-y: auto;

                .menuInActive {
                    margin-left: 16px;

                    .collectTitle {
                        cursor: pointer;
                        width: 200px;

                    }
                }

                .menuActive {
                    background: #262727;

                    .collectTitle {
                        cursor: pointer;
                        width: 184px;
                    }
                }

                .colllectRow {
                    display: flex;
                    align-items: center;
                    color: #FFF;
                    height: 38px;
                    box-sizing: border-box;
                    padding: 8px 20px 8px 4px;
                    font-size: 14px;
                    opacity: 0.9;
                    line-height: 22px;

                    .moveIcon {
                        font-size: 12px;
                        margin-right: 4px;
                    }

                    .closeIcon {
                        font-size: 12px;
                        margin-left: 4px;
                    }

                }
            }
        }

        .right_box {
            width: 100%;
        }
    }

    :deep(.el-drawer) ::-webkit-scrollbar {
        width: 0px !important;
    }
}
</style>